//
//  SearchViewController.m
//  F50
//
//  Created by 郝 巍 on 15/5/6.
//  Copyright (c) 2015年 ZACK. All rights reserved.
//

#import "SearchViewController.h"
#import "CustomButton.h"
#import "RoothomeDataModle.h"
#import "SearchResultViewController.h"
@interface SearchViewController ()<UITextFieldDelegate,UIScrollViewDelegate>
{
    UITextField *searchTF;
    UIScrollView * scrollView;
    NSArray *industry;
    NSString *industryStr;
    NSArray *stage;
    NSString *stageStr;
    NSArray *state;
    NSString *stateStr;
}

@end

@implementation SearchViewController
- (void)createBackground
{
    
    UIImageView *bgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    bgView.image = [UIImage imageNamed:@"background"];
    bgView.userInteractionEnabled = YES;
    [self.view addSubview:bgView];
    
    UIView *nav_bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    nav_bg.backgroundColor = RGB(59, 107, 178);
    [bgView addSubview:nav_bg];
    
    UIImageView *searchVc = [[UIImageView alloc]initWithFrame:CGRectMake(25, 29, WIDTH - 50, 30)];
    searchVc.image = [[UIImage imageNamed:@"searchProject"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    searchVc.backgroundColor = [UIColor whiteColor];
    searchVc.userInteractionEnabled = YES;
    [bgView addSubview:searchVc];
    
    
    searchTF = [[UITextField alloc]initWithFrame:CGRectMake(40, 0, WIDTH - 110, 30)];
    searchTF.backgroundColor = [UIColor whiteColor];
    searchTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    searchTF.delegate = self;
    [searchVc addSubview:searchTF];
    
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    searchBtn.frame = CGRectMake(0, HEIGHT - 89, WIDTH , 40);
    [searchBtn addTarget:self action:@selector(doSearch) forControlEvents:UIControlEventTouchUpInside];
    [searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
    searchBtn.backgroundColor = RGB(59, 107, 178);
    [searchBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    searchBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [bgView addSubview:searchBtn];
    
}

- (void)createScrollView{
    scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64 - 89)];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    
    if (WIDTH > 320) {
        scrollView.contentSize = CGSizeMake(WIDTH, HEIGHT - 89 - 64);
    }else{
        scrollView.contentSize = CGSizeMake(WIDTH, HEIGHT - 89);
    }
    
    scrollView.delegate = self;
    scrollView.backgroundColor = [UIColor clearColor];
    [scrollView setScrollEnabled:YES];//设置pageControl
    scrollView.bounces = NO;
    [self.view addSubview:scrollView];
    
    UILabel *indus = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 200, 30)];
    indus.text = @"行业";
    indus.textColor = [UIColor whiteColor];
    indus.font = [UIFont fontWithName:@"STHeitiK-Medium" size:18];
    [scrollView addSubview:indus];
    
    
    
    industry = [NSArray arrayWithObjects:@"",@"P2P",@"海外购",@"教育",@"无人机",@"医疗机构", @"无线网络应用",nil];
    industryStr = industry[0];
    [CustomButton initWithArray:industry addSuper:scrollView withHeight:46 withTag:1000 addTarget:self action:@selector(doChoiceIndustry:)];
    
    
    UILabel *stageLab = [[UILabel alloc]initWithFrame:CGRectMake(15, 175, 200, 30)];
    stageLab.text = @"融资阶段";
    stageLab.textColor = [UIColor whiteColor];
    stageLab.font = [UIFont fontWithName:@"STHeitiK-Medium" size:18];
    [scrollView addSubview:stageLab];
    
    
    
    stage = [NSArray arrayWithObjects:@"",@"天使期",@"早期",@"成熟期",nil];
    stageStr = stage[0];
    [CustomButton initWithArray:stage addSuper:scrollView withHeight:210 withTag:1007 addTarget:self action:@selector(doChoiceStage:)];
    
    UILabel *stateLab = [[UILabel alloc]initWithFrame:CGRectMake(15, 295, 200, 30)];
    stateLab.text = @"状态";
    stateLab.textColor = [UIColor whiteColor];
    stateLab.font = [UIFont fontWithName:@"STHeitiK-Medium" size:18];
    [scrollView addSubview:stateLab];
    
    
    
    state = [NSArray arrayWithObjects:@"",@"未上线",@"已运营",@"已盈利",@"已关闭",nil];
    stateStr = state[0];
    [CustomButton initWithArray:state addSuper:scrollView withHeight:330 withTag:1011 addTarget:self action:@selector(doChoiceState:)];
}

- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;

    
}
-(void)viewDidAppear:(BOOL)animated{


    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    [self createBackground];
    [self createScrollView];

}



/*
 以下三个方法是用来判断用户在这个界面选择了那些按钮，只能通过tag来判断点击的那个按钮
 tag是前面穿件button的时候给定的 是事先设定好的
 */
-(void)doChoiceIndustry:(UIButton *)sender{
    NSLog(@"%ld",(long)sender.tag);
    
    
    for (int tag = 1000; tag < 1007; tag ++) {
       UIButton *button = (UIButton *)[self.view viewWithTag:tag];
        [button setBackgroundColor:[UIColor clearColor]];
    }
    
    UIButton *button = (UIButton *)[self.view viewWithTag:sender.tag];
    [button setBackgroundColor:RGB(76, 187, 251)];
    industryStr = industry[sender.tag - 1000];
    
}

- (void)doChoiceStage:(UIButton *)sender{
    stageStr = stage[sender.tag - 1007];
    for (int tag = 1007; tag < 1011; tag ++) {
        UIButton *button = (UIButton *)[self.view viewWithTag:tag];
        [button setBackgroundColor:[UIColor clearColor]];
    }
    
    UIButton *button = (UIButton *)[self.view viewWithTag:sender.tag];
    [button setBackgroundColor:RGB(76, 187, 251)];
}
- (void)doChoiceState:(UIButton *)sender{
    stateStr = state[sender.tag -1011];
    for (int tag = 1011; tag < 1016; tag ++) {
        UIButton *button = (UIButton *)[self.view viewWithTag:tag];
        [button setBackgroundColor:[UIColor clearColor]];
    }
    
    UIButton *button = (UIButton *)[self.view viewWithTag:sender.tag];
    [button setBackgroundColor:RGB(76, 187, 251)];
}
/*
 以上三个方法是用来判断用户在这个界面选择了那些按钮，只能通过tag来判断点击的那个按钮
 tag是前面穿件button的时候给定的 是事先设定好的
 */

/* 开始进行搜索*/
-(void)doSearch{
    
    SearchResultViewController *searchRst = [[SearchResultViewController alloc]init];
    searchRst.titleStr = ([industryStr isEqualToString:@""])?@"全部":industryStr;
    searchRst.searchStr = searchTF.text;
    searchRst.industryStr = industryStr;
    searchRst.stageStr = stageStr;
    searchRst.stateStr = stateStr;
    [self.navigationController pushViewController:searchRst animated:YES];
    
    
//    NSDictionary * date = [[NSDictionary  alloc]initWithObjectsAndKeys:[USER_DEFAULT objectForKey:@"token"],@"token",searchTF.text,@"keywords",industryStr,@"industry",stageStr,@"stage",stateStr,@"state",nil];
//    NSDictionary *pram = [[NSDictionary alloc]initWithObjectsAndKeys:@"search",@"class",@"search",@"func",date,@"param", nil];
//    
//    [[AFCustomObject shareAFCustomObject] doRequestActionWithParams:pram success:^(NSDictionary *result) {
//        NSLog(@"%@",[result objectForKey:@"errMsg"]);
//        NSLog(@"%@",result);
//
//        NSArray *array = [result objectForKey:@"result"];
//        NSMutableArray *resultArr = [[NSMutableArray alloc]init];
//        
//        for (int i = 0; i < array.count; i++) {
//            NSDictionary *dict=array[i];
//            RoothomeDataModle *modle=[[RoothomeDataModle alloc] init];
//            modle.title = [dict objectForKey:@"title"];
//            modle.imageUrl = [dict objectForKey:@"thumb"];
//            modle.projectID = [dict objectForKey:@"id"];
//            modle.releasetime = [dict objectForKey:@"releasetime"];
//            modle.info = [dict objectForKey:@"description"];
//            modle.stage = [dict objectForKey:@"stage"];
//            modle.state = [dict objectForKey:@"state"];
//            modle.relations = [dict objectForKey:@"relations"];
//            modle.deals = [dict objectForKey:@"deals"];
//            modle.amount = [dict objectForKey:@"amount"];
//            modle.isrelation = [dict objectForKey:@"isrelation"];
//
//            [resultArr addObject:modle];
//        }
//       
//        
//        
//        
//    } fail:^{
//        
//    }];
    

}



#pragma mark --------- textFieldDelegate
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{
    
    // When the user presses return, take focus away from the text field so that the keyboard is dismissed.
    
    
    [textField resignFirstResponder];
    
    return YES;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
